function getPortExpression(port)
{
  var expr = "";
	try
	{
		expr = evalForExpr(port);
	}
	catch(e)
	{
		alert(e);
	}	
	
	return expr;
}

function evalForExpr(port)
{
	var equationForPort = portEquation[port];
	var expr = "";
	
	var i;
	for(i=0;i<equationForPort.length;i++)
	{
		var c = equationForPort[i];
		if(!isOperator(c))
		{
			var connSrc = getConnectionSrc(c);
			if( connSrc != null )
				expr += evalForExpr(connSrc);
			else
				expr += c ;
		}                               
		else
			expr += ' ' + c + ' ';
	}
	
	return '(' + expr + ')';
}


var portValues;
function evaluatePort(port)
{
	var unusedInputPorts = getAttachedUnusedInputPorts(port);
	var unusedInputPortNo = unusedInputPorts.length;
  var combinationCnt = getCombinationCnt(unusedInputPortNo);
  portValues = new Object();
  var elem = document.getElementById("truthTable");
  var html = "<TABLE class='truth-table'>"; 
    html += getTruthTableHeader(unusedInputPorts, port);
	//try
	//{  
    for(combiVal = 0; combiVal < combinationCnt; combiVal++ )
    {
      //alert(combiVal);
      setPortValues(unusedInputPorts, combiVal);
      html += writeValues(eval(port));
    }
    html += '</TABLE>';
    elem.innerHTML = html;
	/*}
	catch(e)
	{
		alert(e);
	}	*/
}

function getCombinationCnt(prtNo)
{
  var i, combiNo = 1;
  for(i=0;i<prtNo;i++)
  {
    combiNo *= 2;
  }  
  return combiNo;
} 

function setPortValues(unusedInputPorts, combiVal)
{
  var boolCombiVal = getBoolDigits(combiVal, unusedInputPorts.length);
  var i = 0;
  for(i=0;i<unusedInputPorts.length;i++)
  {
    portValues[unusedInputPorts[i]] = boolCombiVal[i];
  }
}
 
function eval(port)
{
	var equationForPort = portEquation[port];
	
	var i, ipCnt=0;
	var inputs, oper;
	
	inputs = new Array();
	for(i=0;i<equationForPort.length;i++)
	{
		var c = equationForPort[i];
		if(!isOperator(c))
		{
			var connSrc = getConnectionSrc(c);
			if( connSrc != null )
				inputs[ipCnt++] = eval(connSrc);
			else
				inputs[ipCnt++] = getPortVal(c);
		}                               
		else
			oper = c;
	}
	
	return calcValue(inputs, oper);
}

function getBoolDigits(combiVal, noOfDigits)
{
  var binaryString = combiVal.toString(2);
  var i=0, strIndex=0;

  var digits = new Array();
  for(i = 0;i<noOfDigits-binaryString.length; i++)
  {
    digits[i] =0;
  }
  
  for(;i<noOfDigits; i++)
  {
    digits[i] =binaryString.charAt(strIndex++);
  }

  return digits;
}

function getPortVal(port)
{
  return portValues[port];
}

function calcValue(inputs, oper)
{
  if(oper == '.')
    return inputs[0] & inputs[1];
  else if(oper == '+')
    return inputs[0] | inputs[1];
  else if(oper == '^')
    return inputs[0] ^ inputs[1];      
}

function isOperator( charecter )
{
	if( charecter == '+' || charecter == '.' || charecter == '^' )
		return true;
		
	return false;
}

function writeValues(output)
{
  var html = '<TR>';
  
  for(var input in portValues)
  {
     html += '<TD>' + portValues[input] + '</TD>';
  }
  
  html += '<TD>' + output + '</TD></TR>';
    
  return html;
  
}

function getTruthTableHeader(inputPorts, outputPort)
{
  var html = "<TR>";
  for(var i=0; i< inputPorts.length; i++)
    html += "<TH class='truth-table-header'>" + inputPorts[i] + '</TH>';
  html += "<TH class='truth-table-header'>" + outputPort +'</TH></TR>';
  
  return html;  
}